package com.supermap.dialogs;

import com.supermap.Helper;
import com.supermap.analyst.spatialanalyst.Generalization;
import com.supermap.data.*;
import com.supermap.data.Enum;
import com.supermap.dialogs.advance.CJDialog;

import javax.swing.*;
import java.awt.*;

public class AggregatePolygonsDialog extends CJDialog {
    private Workspace workspace;

    private JComboBox<String> srcDatasourceCmb;
    private JComboBox<String> srcDatasetCmb;
    private JTextField distanceTxt;
    private JComboBox<Unit> distanceUnitCmb;
    private JTextField minAreaTxt;
    private JTextField minHoleAreaTxt;
    private JComboBox<String> aggregateFeildCmb;
    private JComboBox<String> retDatasourceCmb;
    private JTextField retDatasetTxt;

    private ProcessBarDialog processBarDialog;

    public AggregatePolygonsDialog(Frame owner, String title, boolean modal, Workspace workspace) {
        super(owner, title, modal, 360, 400, new Object[]{workspace});
    }

    @Override
    protected void initializeArgs(Object[] args) {
        workspace = (Workspace) args[0];
    }

    @Override
    protected void initializeCore() {
        setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);

        JPanel centerPanel = new JPanel();
        centerPanel.setLayout(new GridBagLayout());
        add(centerPanel, BorderLayout.CENTER);

        JPanel srcDataPanel = new JPanel();
        srcDataPanel.setBorder(BorderFactory.createTitledBorder("源数据"));
        srcDataPanel.setLayout(new GridBagLayout());
        centerPanel.add(srcDataPanel, new GridBagConstraints(0, 0, 1, 1, 1, 1,
                GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, Helper.DefaultInsets, 0, 0));

        JLabel srcDatasourceLbl = new JLabel("数据源:");
        srcDataPanel.add(srcDatasourceLbl, new GridBagConstraints(0, 0, 1, 1, 4, 1,
                GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, Helper.DefaultInsets, 0, 0));

        srcDatasourceCmb = new JComboBox<>();
        Datasources datasources = workspace.getDatasources();
        int datasourcesCount = datasources.getCount();
        if (datasourcesCount > 0) {
            for (int i = 0; i < datasourcesCount; i++) {
                Datasource datasource = datasources.get(i);
                srcDatasourceCmb.addItem(datasource.getAlias());
            }
            srcDatasourceCmb.setSelectedIndex(0);
        }
        srcDatasourceCmb.addItemListener(e -> {
            String selectedDatasourceName = (String) e.getItem();
            Datasource datasource = datasources.get(selectedDatasourceName);
            loadDatasets(srcDatasetCmb, datasource);
        });
        srcDataPanel.add(srcDatasourceCmb, new GridBagConstraints(1, 0, 1, 1, 10, 1,
                GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, Helper.DefaultInsets, 0, 0));

        JLabel srcDatasetLbl = new JLabel("数据集:");
        srcDataPanel.add(srcDatasetLbl, new GridBagConstraints(0, 1, 1, 1, 4, 1,
                GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, Helper.DefaultInsets, 0, 0));

        srcDatasetCmb = new JComboBox<>();
        if (datasourcesCount > 0) {
            String selectedDatasourceName = (String) srcDatasourceCmb.getSelectedItem();
            Datasource datasource = datasources.get(selectedDatasourceName);
            loadDatasets(srcDatasetCmb, datasource);
            srcDatasetCmb.setSelectedIndex(0);
        }
        srcDatasetCmb.setSelectedIndex(0);
        srcDataPanel.add(srcDatasetCmb, new GridBagConstraints(1, 1, 1, 1, 10, 1,
                GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, Helper.DefaultInsets, 0, 0));

        JPanel parametersPanel = new JPanel();
        parametersPanel.setBorder(BorderFactory.createTitledBorder("参数设置"));
        parametersPanel.setLayout(new GridBagLayout());
        centerPanel.add(parametersPanel, new GridBagConstraints(0, 1, 1, 1, 1, 1,
                GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, Helper.DefaultInsets, 0, 0));

        JLabel distanceLbl = new JLabel("聚合限定距离:");
        parametersPanel.add(distanceLbl, new GridBagConstraints(0, 0, 1, 1, 1, 1,
                GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, Helper.DefaultInsets, 0, 0));

        distanceTxt = new JTextField("0");
        parametersPanel.add(distanceTxt, new GridBagConstraints(1, 0, 1, 1, 10, 1,
                GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, Helper.DefaultInsets, 0, 0));

        JLabel distanceUnitLbl = new JLabel("单位:");
        parametersPanel.add(distanceUnitLbl, new GridBagConstraints(0, 1, 1, 1, 1, 1,
                GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, Helper.DefaultInsets, 0, 0));

        distanceUnitCmb = new JComboBox<>();
        Enum[] enums = Unit.getEnums(Unit.class);
        for (Enum anEnum : enums) {
            distanceUnitCmb.addItem((Unit) anEnum);
        }
        parametersPanel.add(distanceUnitCmb, new GridBagConstraints(1, 1, 1, 1, 10, 1,
                GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, Helper.DefaultInsets, 0, 0));

        JLabel minAreaLbl = new JLabel("限定面积:");
        parametersPanel.add(minAreaLbl, new GridBagConstraints(0, 2, 1, 1, 1, 1,
                GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, Helper.DefaultInsets, 0, 0));

        minAreaTxt = new JTextField("0");
        parametersPanel.add(minAreaTxt, new GridBagConstraints(1, 2, 1, 1, 10, 1,
                GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, Helper.DefaultInsets, 0, 0));

        JLabel minHoleAreaLbl = new JLabel("洞限定面积:");
        parametersPanel.add(minHoleAreaLbl, new GridBagConstraints(0, 3, 1, 1, 1, 1,
                GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, Helper.DefaultInsets, 0, 0));

        minHoleAreaTxt = new JTextField("0");
        parametersPanel.add(minHoleAreaTxt, new GridBagConstraints(1, 3, 1, 1, 10, 1,
                GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, Helper.DefaultInsets, 0, 0));

        JLabel aggregateFeildLbl = new JLabel("聚合字段");
        parametersPanel.add(aggregateFeildLbl, new GridBagConstraints(0, 4, 1, 1, 1, 1,
                GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, Helper.DefaultInsets, 0, 0));

        aggregateFeildCmb = new JComboBox<>();
        Datasource tempDatasource = workspace.getDatasources().get((String) srcDatasourceCmb.getSelectedItem());
        DatasetVector datasetVector = (DatasetVector) tempDatasource.getDatasets().get((String) srcDatasetCmb.getSelectedItem());
        FieldInfos fieldInfos = datasetVector.getFieldInfos();
        for (int i = 0; i < fieldInfos.getCount(); i++) {
            aggregateFeildCmb.addItem(fieldInfos.get(i).getName());
        }
        parametersPanel.add(aggregateFeildCmb, new GridBagConstraints(1, 4, 1, 1, 10, 1,
                GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, Helper.DefaultInsets, 0, 0));

        JPanel retDataPanel = new JPanel();
        retDataPanel.setBorder(BorderFactory.createTitledBorder("结果数据"));
        retDataPanel.setLayout(new GridBagLayout());
        centerPanel.add(retDataPanel, new GridBagConstraints(0, 2, 1, 1, 1, 1,
                GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, Helper.DefaultInsets, 0, 0));

        JLabel retDatasourceLbl = new JLabel("数据源:");
        retDataPanel.add(retDatasourceLbl, new GridBagConstraints(0, 0, 1, 1, 4, 1,
                GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, Helper.DefaultInsets, 0, 0));

        retDatasourceCmb = new JComboBox<>();
        if (datasourcesCount > 0) {
            for (int i = 0; i < datasourcesCount; i++) {
                Datasource datasource = datasources.get(i);
                retDatasourceCmb.addItem(datasource.getAlias());
            }
            retDatasourceCmb.setSelectedIndex(0);
        }
        retDatasourceCmb.addItemListener(e -> {
            String selectedDatasourceName = (String) e.getItem();
            Datasource datasource = datasources.get(selectedDatasourceName);
            retDatasetTxt.setText(datasource.getDatasets().getAvailableDatasetName("aggregatePolygonsResult"));
        });
        retDataPanel.add(retDatasourceCmb, new GridBagConstraints(1, 0, 1, 1, 10, 1,
                GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, Helper.DefaultInsets, 0, 0));

        JLabel retDatasetLbl = new JLabel("数据集:");
        retDataPanel.add(retDatasetLbl, new GridBagConstraints(0, 1, 1, 1, 4, 1,
                GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, Helper.DefaultInsets, 0, 0));

        retDatasetTxt = new JTextField();
        if (datasourcesCount > 0) {
            String selectedDatasourceName = (String) retDatasourceCmb.getSelectedItem();
            Datasource datasource = datasources.get(selectedDatasourceName);
            retDatasetTxt.setText(datasource.getDatasets().getAvailableDatasetName("aggregatePolygonsResult"));
        }
        retDataPanel.add(retDatasetTxt, new GridBagConstraints(1, 1, 1, 1, 10, 1,
                GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, Helper.DefaultInsets, 0, 0));
    }

    @Override
    protected void doConfirmCore() {
        if (processBarDialog == null) {
            processBarDialog = new ProcessBarDialog((Frame) getOwner(), "面聚合", true);
            processBarDialog.setValue(0);
        }

        Datasource srcDatasource = workspace.getDatasources().get((String) srcDatasourceCmb.getSelectedItem());
        DatasetVector srcDatasetVector = (DatasetVector) srcDatasource.getDatasets().get((String) srcDatasetCmb.getSelectedItem());
        Datasource retDatasource = workspace.getDatasources().get((String) retDatasourceCmb.getSelectedItem());
        String retDatasetName = retDatasetTxt.getText();

        AggregatePolygonParameter aggregatePolygonParameter = new AggregatePolygonParameter();
        aggregatePolygonParameter.setAggregateFeild((String) aggregateFeildCmb.getSelectedItem());
        aggregatePolygonParameter.setDistance(Double.parseDouble(distanceTxt.getText()));
        aggregatePolygonParameter.setDistanceUnit((Unit) distanceUnitCmb.getSelectedItem());
        aggregatePolygonParameter.setMinArea(Double.parseDouble(minAreaTxt.getText()));
        aggregatePolygonParameter.setMinHoleArea(Double.parseDouble(minHoleAreaTxt.getText()));

        new Thread(()->{
            Generalization.aggregatePolygons(srcDatasetVector, retDatasource, retDatasetName, aggregatePolygonParameter, steppedEvent -> {
                int percent = steppedEvent.getPercent();
                if (processBarDialog != null) {
                    processBarDialog.setValue(percent);
                    if (percent == 100) {
                        processBarDialog.setVisible(false);
                        processBarDialog.dispose();
                        processBarDialog = null;
                    }
                }
            });
        }).start();

        processBarDialog.setVisible(true);
        setVisible(false);
    }

    private void loadDatasets(JComboBox<String> srcDatasetCmb, Datasource datasource) {
        srcDatasetCmb.removeAll();
        Datasets datasets = datasource.getDatasets();
        int datasetCount = datasets.getCount();
        for (int i = 0; i < datasetCount; i++) {
            Dataset dataset = datasets.get(i);
            DatasetType datasetType = dataset.getType();
            if (datasetType == DatasetType.REGION) {
                srcDatasetCmb.addItem(dataset.getName());
            }
        }
    }
}
